"""
网络请求：获取用户信息
"""
import requests
from src.models.user import User
from src.config import settings


def get_user_info():
    """
    获取用户信息
    """
    user = User.get_user(require_login=True)
    if not user:
        return 1001, '未登录'
    try:
        response = requests.get(settings.user_account_url,
                                headers={'token': user.token}, timeout=5)
    except Exception as e:
        return 1002, '网络请求出错'
    res = response.json()
    if response.status_code == 200 and res['code'] == 2000:
        return 1000, res['data']
    elif response.status_code == 401:
        return 1004, '登陆过期！'
    else:
        return 1003, res['message']


def feedback(text):
    """意见反馈"""
    try:
        user = User.get_user()
        resq_data = requests.post(settings.feedback_url, headers={'Authorization': user.token},
                                  data={'text': text}).json()
        if resq_data['code'] == 200:
            return True, resq_data['message']
        else:
            return False, resq_data['message']
    except Exception as e:
        return False, f'提交用户反馈出错：{e}'


if __name__ == '__main__':
    get_user_info()
